/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buscaminas;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class BuskMin {
    int destapados = 0;
    time T;
    JFrame f;
    Panel cuadros;
    bandera bandera;
    Panel orden;
    int[][] mina;

    class llenar {

        public void llenar() {

            mina = new int[7][7];//se llena el arrelgo de 7 x 7

            for (int z = 0; z < 10; z++) {

                int posicionx = new Double(Math.random() * 7).intValue();
                int posiciony = new Double(Math.random() * 7).intValue();


                mina[posicionx][posiciony] = -1;


            }
            int con = 0;
            for (int a = 0; a < 7; a++) {

                for (int b = 0; b < 7; b++) {
                    System.out.print("|" + mina[a][b] + "|");// se imprime el arreglo
                    if (mina[b][a] == -1) {
                        con++;


                    }


                }
                System.out.println("\n");
            }

            if (con != 10) {
                System.out.println(con);

                llenar();
            }


        }

        public void Minas() {


            for (int a = 0; a < 7; a++) {

                for (int b = 0; b < 7; b++) {
                    int colision = 0;
                    try {

                        if (mina[a][b] >= 0) {

                            try {

                                if (mina[a][b + 1] == -1) {
                                    colision++;

                                }
                            } catch (Exception e) {
                            }
                            try {

                                if (mina[a + 1][b + 1] == -1) {
                                    colision++;

                                }
                            } catch (Exception e) {
                            }

                            try {
                                if (mina[a - 1][b + 1] == -1) {
                                    colision++;

                                }

                            } catch (Exception e) {
                            }
                            // la parte de abajo
                            try {
                                if (mina[a + 1][b] == -1) {
                                    colision++;
                                }
                            } catch (Exception e) {
                            }

                            try {
                                if (mina[a - 1][b] == -1) {
                                    colision++;
                                }
                            } catch (Exception e) {
                            }
                            try {
                                // la parte del centro
                                if (mina[a][b - 1] == -1) {
                                    colision++;
                                }
                            } catch (Exception e) {
                            }
                            try {
                                if (mina[a + 1][b - 1] == -1) {
                                    colision++;
                                }
                            } catch (Exception e) {
                            }
                            try {
                                if (mina[a - 1][b - 1] == -1) {
                                    colision++;
                                }
                            } catch (Exception e) {
                            }
                            mina[a][b] = colision;


                        }


                    } catch (Exception e) {
                    }


                }

            }




        }
    }

    class colores extends Canvas implements MouseListener {

        int numinas;
        private Color color;
        int com = 0;

        public colores(int n) {
            color = Color.BLACK;
            this.addMouseListener(this);
            numinas = n;
        }

        @Override
        public void paint(Graphics g) {


            g.setColor(color);
            g.fillRect(0, 0, 50, 50);

            if (color == Color.GREEN) {

                if (numinas == -1) {// cuando tocas una mina
                    
                    try {

                        Image img = null;
                        File F = new File("imagenes/1.jpg");//imagen de bomba
                        img = ImageIO.read(F);
                        g.drawImage(img, 0, 0, this);
                        JOptionPane.showMessageDialog(f, "P E R D I S T E!!");


                    } catch (Exception e) {
                        System.out.println("error");
                    }
                    cuadros.disable();

                    T.com = 0;

                } else {

                    g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 25));
                    g.setColor(Color.BLACK);
                    g.drawString(numinas + "", 13, 25);

                }


            } else if (color == Color.yellow) {

                try {

                    Image img = null;
                    File f = new File("imagenes/band.jpg");//imagen de Bandera
                    img = ImageIO.read(f);
                    g.drawImage(img, 0, 0, this);

                } catch (Exception e) {
                    System.out.println("error");
                }

            }

            if (bandera.ban == 0 && destapados == 39) {//juego ganado

                JOptionPane.showMessageDialog(f, "G A N A S T E!!");
                System.out.println("win");
                f.dispose();
                BuskMin m = new BuskMin();
            }


        }

        public void mouseClicked(MouseEvent e) {

            com = 1;
            if (com == 1) {

                T.com = 1;
            }

            if (e.getButton() == MouseEvent.BUTTON3) {

                if (color == Color.BLACK) {
                    color = Color.YELLOW;
                    bandera.ban--;
                    bandera.repaint();
                } else if (color == Color.YELLOW) {
                    color = Color.BLACK;
                    bandera.ban++;
                    bandera.repaint();
                }

                repaint();

            } else if (e.getButton() == MouseEvent.BUTTON1) {

                if (color == Color.BLACK) {
                    color = Color.GREEN;

                    destapados += 1;

                }

                repaint();
            }

        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }

    public void frame() {


        f.setBackground(Color.GREEN);
        cuadros.setLayout(new GridLayout(7, 7, 1, 1));
        f.setSize(400, 500);
        f.setResizable(false);

        Button reset = new Button("reset");
        reset.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                f.dispose();
                BuskMin m = new BuskMin();


            }
        });
        f.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


        for (int x = 0; x < 7; x++) {

            for (int y = 0; y < 7; y++) {

                colores col = new colores(mina[x][y]);
                cuadros.add(col);

            }


        }



        orden.setLayout(new GridLayout());
        orden.add(bandera);
        orden.add(reset);
        orden.add(T);
        f.add(orden);
        f.add(BorderLayout.NORTH, orden);
        f.add(BorderLayout.CENTER, cuadros);
        f.setLocation(200, 100);
        f.setVisible(true);
    }

    public static void main(String[] args) {


        BuskMin m = new BuskMin();

       

    }

    public BuskMin() {

        cuadros = new Panel();
        f = new JFrame("Buscaminas");
        orden = new Panel();
        T = new time();
        Thread t = new Thread(T);
        t.start();

        bandera = new bandera();
        llenar l = new llenar();
        l.llenar();
        l.Minas();
        frame();
        




    }
}
